package com.ycfmedical.bed.ui.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ycfmedical.bed.R;
import com.ycfmedical.bed.adapter.VideoPlayAdapter;
import com.ycfmedical.bed.bean.Videoinfo;
import com.ycfmedical.bed.widgets.StateView;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.widget.DefaultItemAnimator;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.mvp.XFragment;
import cn.droidlover.xdroidmvp.mvp.XLazyFragment;
import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * Created by Egoist on 2017/1/19.
 */

public class VideoListFragment extends XLazyFragment
{
    
    @BindView(R.id.video_contentLayout)
    XRecyclerContentLayout videoContentLayout;

    private StateView errorView;

    List<Videoinfo> data = new ArrayList<>();

    private VideoPlayAdapter mAdapter;
    
    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_entertain_videolist;
    }
    
    @Override
    public void initData(Bundle savedInstanceState)
    {
        _initPlayerAttr();
        _initVideoData();
        _initAdapter();
        _initRecyclerView();
    }

    // 全屏模式状态栏设置为透明
    private void _initPlayerAttr() {
        JCVideoPlayer.TOOL_BAR_EXIST = true;
        JCVideoPlayer.ACTION_BAR_EXIST = false;
    }

    private void _initVideoData() {
        File file1 = new File(Environment.getExternalStorageDirectory() + File.separator + "Movies" + File.separator + "transformer.mp4");
        File file2 = new File(Environment.getExternalStorageDirectory() + File.separator + "Movies" + File.separator + "pirates.mp4");
        //File file3 = new File(Environment.getExternalStorageDirectory() + File.separator + "Movies" + File.separator + "starwars.mp4");
        Videoinfo item1 = new Videoinfo(file1.getAbsolutePath(),R.raw.ic_thumb_transformer,getString(R.string.video_item_trans));
        Videoinfo item2 = new Videoinfo(file2.getAbsolutePath(),R.raw.ic_thumb_pirates,getString(R.string.video_item_pirates));
        //Videoinfo item3 = new Videoinfo(file2.getAbsolutePath(),R.raw.ic_thumb_starwars,getString(R.string.video_item_starwars));
        data.add(item1);
        data.add(item2);
    }

    private void _initAdapter() {
        if (mAdapter == null) {
            mAdapter = new VideoPlayAdapter(context);
            mAdapter.setData(data);
        }
    }

    private void _initRecyclerView() {

        if (videoContentLayout.getRecyclerView() != null && mAdapter != null) {
            videoContentLayout.getRecyclerView().verticalLayoutManager(context);
            videoContentLayout.getRecyclerView().setAdapter(mAdapter);
            videoContentLayout.getRecyclerView().setItemAnimator(new DefaultItemAnimator());
            videoContentLayout.getRecyclerView().addItemDecoration(new VideoPlayAdapter.SpaceItemDecoration(6));
            videoContentLayout.getRecyclerView().setRefreshEnabled(false);

        }
        if (errorView == null) errorView = new StateView(context);
        videoContentLayout.errorView(errorView);
        videoContentLayout.loadingView(View.inflate(context, R.layout.view_loading, null));
    }

    @Override
    public void onStopLazy()
    {
        super.onStopLazy();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onPauseLazy()
    {
        super.onPauseLazy();
        JCVideoPlayer.releaseAllVideos();
    }

    @Override
    public Object newP()
    {
        return null;
    }
    
    public static VideoListFragment newInstance()
    {
        return new VideoListFragment();
    }
}
